﻿using CarEdge.Infrastructure.Attr;
using CarEdge.Infrastructure.Dto.Request.login;
using CarEdge.Infrastructure.Dto.Response;
using CarEdge.Infrastructure.Tools;
using CarEdge.Service.Interface;
using Crm.Infrastructure.Tools;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace CarEdge.Admin.Controllers.login
{
          /// <summary>
          /// 登录管理
          /// </summary>
    public class LoginController:BaseController
    {
      
        private readonly IConfiguration _configuration;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="login_Service"></param>
        public LoginController(IConfiguration configuration, I_Login_Service login_Service):base(login_Service)
        {
            _configuration = configuration;
        }
        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Action_Log_Attribute(name = "管理员登录", type = "登录", version = "pc")]
        public async Task<IActionResult> Logins(Login_Request_Dto dto)
        {
            dto.code = Config.GUID2();
            var result= await _login_Service.Logins(dto);
            if (result.code == 0)
            {
                Claim[] claims = new Claim[] {
                new Claim(ClaimTypes.Name,dto.code),
                new Claim(ClaimTypes.Role,"Admin")
                };
                var token = JWTUtil.EnJwt(claims, _configuration["JWT:secretKey"]!,
                _configuration["JWT:issuer"]!);
                result.data = token;
            }
            return Ok(result);
        }
        /// <summary>
        /// 检查登录
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> Check_Login()
        {
            var user= await this.Get_Curr_User();
            return Ok(new Api_Response_Dto
            {
                code = Api_Code.ok,
                message = "ok",
                data = new
                {
                    account = user.Uaccount,
                    name = user.Uname
                }
            });
        }
    }
}
